<template>
    <div class="auth-container">
      <h2>用户登录</h2>
      <form @submit.prevent="handleLogin">
        <div class="form-group">
          <label>用户名</label>
          <input v-model="credentials.username" type="text" required>
        </div>
        <div class="form-group">
          <label>密码</label>
          <input v-model="credentials.password" type="password" required>
        </div>
        <button type="submit">登录</button>
      </form>
      <p>没有账号？<router-link to="/register">去注册</router-link></p>
      <div v-if="message" :class="['message', success ? 'success' : 'error']">
        {{ message }}
      </div>
    </div>
  </template>
  
  <script>
  import axios from 'axios'
  
  export default {
    data() {
      return {
        credentials: {
          username: '',
          password: ''
        },
        message: '',
        success: false
      }
    },
    methods: {
      async handleLogin() {
        try {
          const response = await axios.post('http://47.108.137.238:8081/login', this.credentials)
          this.message = response.data.message
          this.success = response.data.success
          if (response.data.success) {
            // 登录成功后的处理（如跳转）
            setTimeout(() => {
              this.$router.push('/dashboard')
            }, 1500)
          }
        } catch (error) {
          this.message = error.response?.data?.message || '登录失败'
          this.success = false
        }
      }
    }
  }
  </script>
  
  <style scoped>
  /* 复用注册页面的样式 */
  </style>